package com.example.shop.goods.dto;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.dto.Avatar;
import com.example.shop.pojo.Evaluation;
import com.example.shop.pojo.EvaluationPicture;
import com.example.shop.user.dto.UserBriefInfo;
import com.example.shop.goods.enums.EvaluationType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Document(indexName = "evaluation-detail-browse-info",createIndex = false)
public class EvaluationBrowseInfo {
    //该评价的唯一标识
    @Id
    protected Long evaluationId;
    //进行评价的用户
    @Field(type = FieldType.Object)
    protected UserBriefInfo evaluationUser;
    //评价类型（好中差）
    protected EvaluationType evaluationType;
    //该评价的点赞数
    protected int thumbUpNumber;
    //点赞用户列表
    protected List<Avatar> thumbUpUserList;
    //该用户购买该商品的订单记录里的商品型号、购买数量、购买了几次了
    @Field(type = FieldType.Object)
    protected UserPurchasedGoods userPurchasedGoods;
    //评价内容
    @Field(type = FieldType.Keyword)
    protected String evaluationContent;
    //评价所用的图片列表
    @Field(type = FieldType.Keyword)
    protected List<String> evaluationPictureList;
    //评价的视频
    @Field(type = FieldType.Keyword)
    protected String evaluationVideo;
    //评价的日期
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd")
    protected LocalDate evaluationDate;

    public void setEvaluationPictureList(List<String> evaluationPictureList) {
        ArrayList<String> requestList = new ArrayList<>();
        for(String resource:evaluationPictureList){
            requestList.add(ResponseResult.toRequestPath(resource));
        }
        this.evaluationPictureList = requestList;
    }

    public void setEvaluationVideo(String evaluationVideo) {
        this.evaluationVideo = ResponseResult.toRequestPath(evaluationVideo);
    }

    public EvaluationBrowseInfo(long evaluationId){
        this.evaluationId = evaluationId;
    }

    @Override
    public String toString() {
        return "EvaluationBrowseInfo{" +
                "evaluationId=" + evaluationId +
                ", evaluationUser=" + evaluationUser +
                ", evaluationType=" + evaluationType +
                ", thumbUpNumber=" + thumbUpNumber +
                ", thumbUpUserList=" + thumbUpUserList +
                ", userPurchasedGoods=" + userPurchasedGoods +
                ", evaluationContent='" + evaluationContent + '\'' +
                ", evaluationPictureList=" + evaluationPictureList +
                ", evaluationVideo='" + evaluationVideo + '\'' +
                ", evaluationDate=" + evaluationDate +
                '}';
    }

    public static EvaluationBrowseInfo getBrowseInfo(UserBriefInfo userBriefInfo, Evaluation evaluation, List<EvaluationPicture> evaluationPictureList, UserPurchasedGoods userPurchasedGoods){
        EvaluationBrowseInfo evaluationBrowseInfo = new EvaluationBrowseInfo();
        evaluationBrowseInfo.setEvaluationId(evaluation.getEvaluationId());
        evaluationBrowseInfo.setEvaluationUser(userBriefInfo);
        evaluationBrowseInfo.setEvaluationType(EvaluationType.valueOf(evaluation.getEvaluationType()));
        evaluationBrowseInfo.setThumbUpUserList(new ArrayList<>());
        evaluationBrowseInfo.setUserPurchasedGoods(userPurchasedGoods);
        evaluationBrowseInfo.setEvaluationContent(evaluation.getEvaluationContent());
        List<String> picturePaths = new ArrayList<>();
        for(EvaluationPicture picture:evaluationPictureList){
            picturePaths.add(picture.getEvaluationPicture());
        }
        evaluationBrowseInfo.setEvaluationPictureList(picturePaths);
        evaluationBrowseInfo.setEvaluationVideo(evaluation.getEvaluationVideo());
        evaluationBrowseInfo.setEvaluationDate(evaluation.getEvaluationDate());
        return evaluationBrowseInfo;
    }
}
